﻿//===============================================================================
// Microsoft Innovation Centre - Western Europe
// Copyright (c) 2008 Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.WVB.Framework;
using Microsoft.WVB.Silverlight.Infrastructure;
using System.Windows.Input;
using Microsoft.Practices.Composite.Events;

namespace Microsoft.WVB.UI
{
    [TemplatePart(Name = SearchVignette.SearchTextName, Type = typeof(TextBox)),
    TemplatePart(Name = SearchVignette.SearchButtonName, Type = typeof(Button)),
    TemplatePart(Name = SearchVignette.MinimizeElementName, Type = typeof(Button))]
    public class SearchVignette : Control
    {
        private const string SearchButtonName = "SearchButton";
        private const string SearchTextName = "SearchTextBox";
        private const string MinimizeElementName = "MinimizeElement";

        private ExplorerBase _ParentControl = null;

        protected Button SearchButton = null;
        protected Button MinimizeButton = null;
        protected TextBox SearchText = null;

        public IExplorerModel ViewModel { get; private set; }

        public SearchVignette(ExplorerBase parentControl)
            : base()
        {
            this.DefaultStyleKey = typeof(SearchVignette);

            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                ViewModel = IoC.GetCreate<IExplorerModel>();
                DataContext = ViewModel;
                this._ParentControl = parentControl;
            }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            RoutedEventHandler searchClick = null;
            KeyEventHandler searchKeyDown = null;
            RoutedEventHandler minimizeHandler = null;

            SearchButton = base.GetTemplateChild(SearchVignette.SearchButtonName) as Button;
            SearchText = base.GetTemplateChild(SearchVignette.SearchTextName) as TextBox;
            MinimizeButton = base.GetTemplateChild(SearchVignette.MinimizeElementName) as Button;

            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                if ((SearchButton != null) && (SearchText != null))
                {
                    if (searchClick == null)
                    {
                        searchClick = delegate(object sender, RoutedEventArgs e)
                        {
                            if (!String.IsNullOrEmpty(SearchText.Text))
                            {
                                ViewModel.Search(SearchText.Text);
                                if ((this._ParentControl != null) && (this._ParentControl.CurrentState == ExplorerState.Iconed))
                                {
                                    this._ParentControl.CurrentState = ExplorerState.Normal;
                                    IEventAggregator ag = IoC.GetCreate<IEventAggregator>();
                                    ag.GetEvent<ExplorerMinimizeEvent>().Publish(_ParentControl);
                                }
                            }
                        };
                    }
                    SearchButton.Click += searchClick;

                    // Listen for Enter button press OnKeyDown
                    searchKeyDown = delegate(object sender, System.Windows.Input.KeyEventArgs e)
                    {
                        if (e.Key == System.Windows.Input.Key.Enter)
                        {
                            if (!String.IsNullOrEmpty(SearchText.Text))
                            {
                                // Enter key pressed and text has been entered 
                                ViewModel.Search(SearchText.Text);
                                if ((this._ParentControl != null) && (this._ParentControl.CurrentState == ExplorerState.Iconed))
                                {
                                    this._ParentControl.CurrentState = ExplorerState.Normal;
                                    IEventAggregator ag = IoC.GetCreate<IEventAggregator>();
                                    ag.GetEvent<ExplorerMinimizeEvent>().Publish(_ParentControl);
                                }
                            }
                        }
                    };

                    SearchText.KeyDown += searchKeyDown;

                }

                if (MinimizeButton != null)
                {
                    if (minimizeHandler == null)
                    {
                        minimizeHandler = delegate(object sender, RoutedEventArgs e)
                        {
                            if (this._ParentControl.CurrentState == ExplorerState.Normal)
                                this._ParentControl.CurrentState = ExplorerState.Iconed;
                        };
                    }
                    MinimizeButton.Click += minimizeHandler;
                }
            }
        }
    }
}
